package itheima.com.threadatom3;

import java.util.concurrent.atomic.AtomicInteger;

public class MyAtomIntegerDemo2 {
    public static void main(String[] args) {
        //直接获取默认值
        AtomicInteger ac1 = new AtomicInteger();
        System.out.println(ac1.get()); //0

        //ac2.getAndIncrement() 将当前值加一，返回值为自增前的值
        AtomicInteger ac2 = new AtomicInteger(10);
        int andIncrement = ac2.getAndIncrement();
        System.out.println(andIncrement); //10
        System.out.println(ac2);    //11

        //ac3.incrementAndGet() 将当前值加一，返回值为自增后的值
        AtomicInteger ac3 = new AtomicInteger(10);
        int i = ac3.incrementAndGet();
        System.out.println(i); //11
        System.out.println(ac3);    //11

        //将参数与对象中的值相加，并返回结果
        AtomicInteger ac4 = new AtomicInteger(10);
        int i1 = ac4.addAndGet(20);
        System.out.println(i1);
        System.out.println(ac4);

        //将当前值设置为newValue的值 并返回旧值
        AtomicInteger ac5 = new AtomicInteger(10);
        int andSet = ac5.getAndSet(20);
        System.out.println(andSet);
        System.out.println(ac5);
    }
}
